package com.lnking.lnkingplace.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationLeadershipMapper;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.model.dto.isolation.leadership.LeadershipDetailDto;
import com.lnking.lnkingplace.model.dto.isolation.leadership.LeadershipImport;
import com.lnking.lnkingplace.model.entity.IsolationLeadership;
import com.lnking.lnkingplace.model.entity.Leaders;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.isolation.leadership.LeadershipAddRequest;
import com.lnking.lnkingplace.model.request.isolation.leadership.LeadershipUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationLeadershipService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 领导关怀计划 服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-08-31
 */
@Service
public class IsolationLeadershipServiceImpl extends CustomServiceImpl<IsolationLeadershipMapper, IsolationLeadership> implements IIsolationLeadershipService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception{
        ImportOption option = new ImportOption.Builder().build();
        List<LeadershipImport> result =  ImportUtils.importExcel(file, LeadershipImport.class,option);
        if(result.size() > 0){
            this.getBaseMapper().updateDelFlag();
        }
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getName())){
                String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "场所不存在："+"【"+r.getPlaceName()+"】");

                IsolationLeadership info = new IsolationLeadership();
                BeanUtils.copyProperties(r,info);
                info.setPlaceId(placeId);
                String startTime = r.getStartTime();
                if(!startTime.contains(" ")){
                    startTime += " 00:00:00";
                }
                LocalDateTime localDateTime = LocalDateTimeUtil.stringToLocalDateTime(startTime, DateFormat.LOCAL_DATE_TIME);
                info.setStartTime(localDateTime);
                getBaseMapper().insert(info);
            }
        });
    }

    @Override
    public void add(LeadershipAddRequest params) {
        IsolationLeadership leadership=new IsolationLeadership();
        BeanUtils.copyProperties(params,leadership);
        getBaseMapper().insert(leadership);
    }

    @Override
    public void update(LeadershipUpdateRequest params) {
        IsolationLeadership leadership=new IsolationLeadership();
        BeanUtils.copyProperties(params,leadership);
        getBaseMapper().updateById(leadership);
    }

    @Override
    public LeadershipDetailDto getUserDetailById(String id) {
        return getBaseMapper().getDetail(id);
    }
}
